﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame2
{
    class Player: RotatedRectangle
    {
        private const int MAX_VELOCITY = 10;
        private const int MAX_ROT_DEGREES = 35;

        //min and max  pos values
        private Vector2 m_boundsX;
        private Vector2 m_boundsY;

        private float m_speed;
        private float m_velocity;
        private float m_resetRotationSpeed;
        private bool m_resetRotation;
        private bool m_isRotating;

        public bool ResetRotation 
        {
            get { return m_resetRotation; }
            set { m_resetRotation = value; }
        }

        public bool IsRotating
        {
            get { return m_isRotating; }
            set { m_isRotating = value; }
        }

        public float Velocity
        {
            get { return m_velocity; }
            set { m_velocity = value; }
        }

        public Player(Rectangle a_rect, float a_initialRotation)
            : base(a_rect, a_initialRotation)
        {
            m_velocity = 0.0f;
            m_speed = 5;
            m_resetRotation = false;
            m_resetRotationSpeed = 0.1f;
        }

        public Vector2 BoundsX 
        {
            get { return m_boundsX; }
            set { m_boundsX = value; }
        }

        public Vector2 BoundsY
        {
            get { return m_boundsY; }
            set { m_boundsY = value; }
        }

        public void Update(GameTime a_gameTime, int a_screenHeight)
        {
            if (this.Y < 0)
                this.Y = 0;
            if (this.Y + this.Height > a_screenHeight)
                this.Y = a_screenHeight - this.Height;

            if (m_resetRotation)
            {
                if (this.Rotation != 0)
                {
                    if (this.Rotation - m_resetRotationSpeed >= 0)
                        this.Rotation -= m_resetRotationSpeed;
                    else if (this.Rotation + m_resetRotationSpeed <= 0)
                        this.Rotation += m_resetRotationSpeed;
                    else
                        this.Rotation = 0;
                }
                else
                {
                    m_resetRotation = false;
                }
            }
        }

        public void Rotate(float a_amount, GameTime a_gameTime)
        {
            if (a_amount != 0)
            {
                float radians = MathHelper.ToRadians(MAX_ROT_DEGREES);
                this.Rotation = MathHelper.Clamp(this.Rotation + a_amount, -radians, radians);
            }
            //if (a_amount > 0)
            //{
            //    if (MathHelper.ToDegrees(this.Rotation + a_amount) <= MAX_ROT_DEGREES)
            //    {
            //        this.Rotation += a_amount;
            //        m_isRotating = true;
            //    }
            //    else
            //    {
            //        m_isRotating = false;
            //        this.Rotation = MathHelper.ToRadians(MAX_ROT_DEGREES);
            //    }
            //}
            //else
            //{
            //    if (MathHelper.ToDegrees(this.Rotation + a_amount) >= -MAX_ROT_DEGREES)
            //    {
            //        m_isRotating = true;
            //        this.Rotation += a_amount;
            //    }
            //    else
            //    {
            //        m_isRotating = false;
            //        this.Rotation = MathHelper.ToRadians(-MAX_ROT_DEGREES);
            //    }
            //}
        }

        public void Rotate(float a_rotation)
        {
           
            //radians = (float)Math.Sinh(radians);
            if (a_rotation != 0)
            {
                float radians = MathHelper.ToRadians(MAX_ROT_DEGREES);
                this.Rotation = MathHelper.Clamp(a_rotation, -radians, radians);
                //if (MathHelper.ToDegrees(this.Rotation) >= MAX_ROT_DEGREES)
                //{
                //    this.Rotation = MathHelper.ToRadians(MAX_ROT_DEGREES);
                //}
                //else if (MathHelper.ToDegrees(this.Rotation) <= -MAX_ROT_DEGREES)
                //{
                //    this.Rotation = MathHelper.ToRadians(-MAX_ROT_DEGREES);
                //}
                //else
                //{
                //    this.Rotation = a_rotation;
                //}
            }            
        }

        public override void ChangePosition(int theXPositionAdjustment, int theYPositionAdjustment)
        {
            base.ChangePosition(theXPositionAdjustment, theYPositionAdjustment);
        }
    }
}
